<?php

namespace app\api\controller;

use think\Db;
use think\Request;
use app\api\model\Attention;
use app\api\model\Blacklist;
use app\api\model\ConfigSystem as Config;
use app\api\model\OrderCommon;
use app\api\model\User;
use app\api\model\UserDeedLimit;
use app\api\model\UserImg;
use app\api\model\UserMaterialVerify;
use app\api\model\UserProfile;
use app\api\model\UserSelectionConditions;
use app\service\FaceAuth;

class Matchmaking extends Common
{
    public function __construct(Request $request = null) {
        parent::__construct($request);
    }

    public function init()
    {
        UserProfile::initd($this->member['id']);
        return $this->pass('');
    }
    public function index(Request $request = null)
    {
        $blacklist = Blacklist::lists($this->member['id'], false, true);
        $kefu = Db::name('config_system')->where(['title'=>'id'])->value('content');
        $blacklist[] = $kefu;

        $where = "hide_search = 0 and img_auth <> 3 and sandbox=0 and delete_time=0 and is_banned=0 and entertainment != 0";

        $attention = Attention::lists($this->member['id'], false, true);

        $tuijian = input('tuijian/d', 0);

        if ($request->param('attention')) {
            $attention_str = implode(',', array_diff($attention, $blacklist));
            $attention_str = $attention_str ?: 0;
            $where .= " and u.id in ($attention_str)";
        } else {
            $blacklist_str = implode(',', $blacklist);
            $where .= " and u.id not in ($blacklist_str)";
        }

        $sex = $request->param('sex') ? $request->param('sex') : ($this->member['sex'] == '男' ? '女' : '男');
        $where .= " and up.sex = '$sex'";

        if ($request->param('name')) {
            $where .= " and (up.name LIKE '%" . $request->param('name') . "%')";
        }

        if ($request->param('city')) {
            $where .= " and (up.city LIKE '%" . $request->param('city') . "%' or notlimit_city <> 0)";
        }

        if ($request->param('vip')) {
            $where .= " and noble_grade > 0";
        }

        if ($request->param('is_identity_authentication')) {
            $where .= " and is_identity_authentication = 2";
        }

        if ($request->param('nvshen')) {
            $where .= " and nvshen = 1";
        }

        if ($request->param('charm')) {
            $where .= " and attention >= 10";
        }

        // 精选
        if ($request->param('chosen')) {
            $where .= " and is_chosen = 1";
        }

        $contain_equal = ['phone', 'constellation', 'city', 'through_membership', 'is_house_purchase',
            'is_car_purchase', 'monthly_salary', 'when_marry', 'had_kids', 'emotion', 'want_kids', 'is_smoking', 'is_drinking'];
        $contain_between = ['age', 'stature', 'weight'];
        $contain_in = ['education_background', 'occupation'];

        $userTableFields = Db::getTableInfo('user', 'fields');
        $userProfileTableFields = Db::getTableInfo('user_profile', 'fields');

        $contain_equal_conditions = [];
        $contain_between_conditions = [];
        $contain_in_conditions = [];
        if ($tuijian) {
            $conditions = Db::name('user_selection_conditions')->where(['user_id' => $this->member['id']])->find();
            if ($conditions) {
	            foreach ($conditions as $key => $item) {
	                // if (in_array($key, $contain_equal) && $item) {
	                //     $contain_equal_conditions[$key] = $item;
	                // }

	                if (in_array($key, $contain_between) && $item) {
	                    $contain_between_conditions[$key] = $item;
	                }

	                if (in_array($key, $contain_in) && $item) {
	                    $contain_in_conditions[$key] = $item;
	                }
	            }
            }
        }

        $where_conditions = '';
        $where_conditions_array = [];
        $where_conditions_array_or = [];
        foreach ($contain_equal as $item) {
        	if (in_array($item, $userProfileTableFields)) {
	            if ($request->param($item)) {
	            	$where_conditions_array[$item] = $request->param($item);
	            } else if (array_key_exists($item, $contain_equal_conditions)) {
	            	$where_conditions_array[$item] = $contain_equal_conditions[$item];
	            }
	            continue;
        	}
            if ($request->param($item)) {
                $where .= " and $item = '" . $request->param($item) . "'";
            } else if (array_key_exists($item, $contain_equal_conditions)) {
                $where_conditions .= " or $item = '" . $contain_equal_conditions[$item] . "'";
            }
        }

        foreach ($contain_between as $item) {
        	if (in_array($item, $userProfileTableFields)) {
	            if ($request->param($item)) {
                	$item_array = explode(',', $request->param($item));
	            	$where_conditions_array[$item] = count($item_array) > 1 ? ['between', [$item_array[0], $item_array[1]]] : ['>', $item_array[0]];
	            } else if (array_key_exists($item, $contain_equal_conditions)) {
                	$item_array = explode(',', $contain_between_conditions[$item]);
	            	$where_conditions_array_or[$item] = count($item_array) > 1 ? ['between', [$item_array[0], $item_array[1]]] : ['>', $item_array[0]];
	            }
	            continue;
        	}
            if ($request->param($item)) {
                $where .= count($item_array) > 1 ? " and ($item between $item_array[0] and $item_array[1])" : " and $item > $item_array[0]";
            } else if (array_key_exists($item, $contain_between_conditions)) {
                $where_conditions .= count($item_array) > 1 ? " or ($item between $item_array[0] and $item_array[1])" : " or $item > $item_array[0]";
            }
        }

        $inList = [
        	'education_background'=>[
	        	'高中以下' => ['初中','中专'],
	        	'高中及以上' => ['高中','大专','本科','双学士','硕士','博士'],
	        	'大专及以上' => ['大专','本科','双学士','硕士','博士'],
	        	'本科及以上' => ['本科','双学士','硕士','博士'],
	        	'硕士及以上' => ['硕士','博士'],
	        	'博士' => ['博士'],
	        ], 
	        'occupation'=>[]
        ];
        foreach ($contain_in as $item) {
        	if (in_array($item, $userProfileTableFields) && in_array($item, array_keys($inList))) {
	            if ($request->param($item)) {
                	$item_array = $inList[$item][$request->param($item)]??($request->param($item)?explode(',', $request->param($item)):[]);
	            	$where_conditions_array[$item] = ['in', $item_array];
	            } else if (array_key_exists($item, $contain_equal_conditions)) {
                	$item_array = $inList[$item][$contain_in_conditions[$item]]??($contain_in_conditions[$item]?explode(',', $contain_in_conditions[$item]):[]);
	            	$where_conditions_array_or[$item] = ['in', $item_array];
	            }
	            continue;
        	}
        }

        if (sizeof($where_conditions_array) || sizeof($where_conditions_array_or)) {
        	$uids = Db::name('user_profile')->where($where_conditions_array)->where(function ($query) use ($where_conditions_array_or) {
        		$query->whereOr($where_conditions_array_or);
        	})->column('user_id');
        	if (empty($uids)) return $this->pass([], 'result');
        	$where .= " and u.id in (".implode(',', $uids).")";
        }

        if ($where_conditions) {
            $where_conditions = ltrim($where_conditions, ' or ');

            $where .= " and ($where_conditions)";
        }

        if ($request->param('distance')) {
            $where .= " having distance <=" . $request->param('distance');
        }

        $order = '';

        if ($request->param('hot')) {
            $order = "attention desc, ";
        }
        
        if ($request->param('new')) {
            $order = "up.id desc, ";
        }

        if ($request->param('online')) {
            $order = "is_online desc, ";
        }

        $order .= 'distance asc';

        $page = isset($this->params['page']) ? $this->params['page'] : 1;
        $pagesize = isset($this->params['pagesize']) ? $this->params['pagesize'] : 10;

        if ($request->param('greet')) {
            $where .= " having distance <= 500000";
            $order = 'rand()';
            $pagesize = 9;
        }

        $distance_sql = distance($request->param('latitude'), $request->param('longitude'));

        $field = 'user_id, up.name user_name, up.head_portrait user_head, up.sex user_sex, up.age user_age, up.city, offten_city, up.occupation, up.constellation, 
                img_auth, is_online, is_identity_authentication, hide_online_time, last_login_datetime, nvshen, 
                u.vip_end_datetime, hide_distance, kejian, xuni_discount, notlimit_city, role, up.stature, up.weight, 
                up.education_background, up.monthly_salary, up.proxy, up.introduction, up.emotion, up.through_membership, up.is_car_purchase, up.is_house_purchase, up.when_marry, up.had_kids, up.want_kids, up.is_smoking, up.is_drinking, up.animal, u.is_identity_authentication, u.is_education_authentication, u.house_auth, u.car_auth';

        $sql = "select $field, $distance_sql from user_profile up inner join user u on up.user_id=u.id where $where order by $order limit " . ($page - 1) * $pagesize . ", $pagesize";
        // var_dump($sql);exit;

        $data = Db::query($sql);

        $imgCount = $data ? UserImg::lists(array_column($data, 'user_id')) : [];

        if ($tuijian) {
            $user_selection_conditions = Db::name('user_selection_conditions')->where(['user_id'=>['in', array_column($data, 'user_id')]])->column('*', 'user_id');
        }

        foreach ($data as $key => $item) {

            $data[$key]['is_vip'] = strtotime($item['vip_end_datetime']) > time() ? 1 : 0;

            if ($item['hide_distance']) {
                $data[$key]['user_distance'] = '保密';
            } else {
                $distance = $item['xuni_discount'] + 0 ? $item['xuni_discount'] : $item['distance'];
                $data[$key]['user_distance'] = $distance >= 1000 ? number_format(($distance / 1000), 1) . 'km' : number_format($distance, 1) . 'm';
            }

            $is_online = $item['is_online'];
            $data[$key]['is_online'] = ($item['role'] == 1 && !$is_online) ? 1 : ($is_online == -1 ? 0 : $is_online);

            $data[$key]['love'] = in_array($item['user_id'], $attention) ? 1 : 0;

            $data[$key]['city'] = $item['notlimit_city'] ? $this->member['city'] : $item['city'];

            $data[$key]['last_login_datetime'] = changeTime($data[$key]);

            $data[$key]['imgcount'] = array_key_exists($item['user_id'], $imgCount) ? $imgCount[$item['user_id']] : 0;

            $data[$key]['user_selection_conditions'] = $user_selection_conditions[$item['user_id']] ?? false;

            unset($data[$key]['img_auth'], $data[$key]['vip_end_datetime'], $data[$key]['xuni_discount'],
                $data[$key]['notlimit_city'], $data[$key]['distance'], $data[$key]['offten_city']);
        }

        return $this->pass($data, 'result');
    }


    public function edit_conditions()
    {
        if (empty($this->params)) {
            return $this->return['common.success.update'];
        }

        $user_id = $this->member['id'];

        $conditions = UserSelectionConditions::get(['user_id' => $user_id]);

        $current_time = time();

        $data = array_merge($this->params, [
            'user_id' => $user_id,
            'update_time' => $current_time
        ]);

        if ($conditions) {
            $method = 'update';
            $data['id'] = $conditions->id;
        } else {
            $data['create_time'] = $current_time;
            $method = 'create';
        }

        UserSelectionConditions::$method($data);

        return json(['code' => 200, 'msg' => '修改成功']);
    }


    public function orders()
    {
        $page = input('page/d', 1);
        $pagesize = input('pagesize/d', 10);

        $data = Db::name('order_common')
        	->where(['status' => 1, 'user_id' => $this->member['id']])
        	->page($page, $pagesize)
        	->field('id,types,number,money,pays,update_time')
            ->order('id desc')
        	->select();

        $type_text = ['1' => '发红包', '2' => '连麦', '4' => '礼物', '5' => '座驾', '6' => '贵族', '7' => '短视频', '8' => '人工牵线VIP', '9' => '开通婚介', '10' => '申请合伙人'];
        foreach ($data as $k => $v) {
            $data[$k]['update_time'] = _time($v['update_time']);
            $data[$k]['type_text'] = $type_text[$v['types']] ?? '';
        }

        return $this->pass($data);
    }


    public function team(Request $request = null)
    {
        $level = $request->param('level') ?: 1;
        $proxy = $request->param('proxy') ?: 0;
        $page = input('page/d', 1);
        $pagesize = input('pagesize/d', 20);
        $pagesize = $pagesize > 20 ? 20 : $pagesize;


        $user = $this->member;

        $contain = [$user['yqm']];

        if ($level) {
            for ($i = 0; $i < $level; $i++) {
                $contain = Db::name('user')->where(['yqr' => ['in', $contain]])->column('yqm');

                if (!$contain) {
                    break;
                }
            }
        }

        $where = [
            'u.id' => ['neq', $user['id']],
            'u.yqm' => ['in', $contain],
            'up.proxy' => $proxy
        ];

        $data = Db::name('user_profile')
            ->alias('up')
            ->join('user u', 'u.id=up.user_id')
            ->where($where)
            ->field('u.id, up.name, up.head_portrait head_300, u.register_datetime')
            ->order('u.id desc')
            ->page($page)
            ->limit($pagesize)
            ->select();

        foreach ($data as $key => $item) {
            //$data[$key]['is_vip'] = strtotime($item['vip_end_datetime']) > time() ? 1 : 0;
            //$data[$key]['invite_time'] = date('Y-m-d H:i:s', $item['invite_time']);
            $data[$key]['shouyi'] = Db::name('yaoqing_records')->where([
                'uid' => $request->param('id'),
                'touid' => $item['id'],
                'content' => ['like', '%收益']
            ])->cache(600)->sum('money');

            $result = Db::name('order_common')->where(['types' => 6, 'user_id' => $item['id'], 'status' => 1])->find();
            if (!$result) {
                $result = Db::name('topup')->where(['state' => 1, 'fk_userid' => $item['id']])->find();
            }

            $data[$key]['had_recharge'] = $result ? 1 : 0;
        }

        return json(['code' => 200, 'data' => $data]);
    }


    public function custom_service()
    {
        $data = Db::name('config')->where(['id' => ['gt', 0]])->column('v','k');

        foreach ($data as $k => $v) {
            $v = str_replace('，', ',', $v);
            trim($v, ',');
            $data[$k] = explode(',', $v);
        }


        return $this->pass($data);
    }

    public function get_gift_records()
    {
        $to_user = input('to_user/d', 0);
        $data = Db::name('gift_record')->where(['to_user'=>$to_user])->field('sum(num) num, gift_id')->group('gift_id')->select();
        
        $gift = Db::name('gift')->column('name,preview,price', 'id');
        
        foreach ($data as &$v) {
            $v['gift'] = $gift[$v['gift_id']]??false;
        }
        
        return $this->pass($data);
    }
    public function put_gift_records()
    {
        $to_user = input('to_user/d', 0);
        $data = Db::name('gift_record')->where(['user_id'=>$to_user])->field('sum(num) num, gift_id')->group('gift_id')->select();
        
        $gift = Db::name('gift')->column('name,preview,price', 'id');
        
        foreach ($data as &$v) {
            $v['gift'] = $gift[$v['gift_id']]??false;
        }
        
        return $this->pass($data);
    }

    public function conditions()
    {
        $to_user = input('to_user/d', $this->member['id']);
        $data = UserSelectionConditions::get(['user_id' => $to_user]);

        return $this->pass($data);
    }

    public function editHead()
    {
        $config = Config::where(['title' => ['in', ['check_avatar']]])->column('title, content');
        $face = input('face', 0);
        $head = input('head/s', '');

        $user = $this->member;
        if (!$user) return $this->return['user.error.notExist'];
        if (!$face) {
            $limit_change_age = Db::name('change_age_limit')->where(['user_id'=>$this->member['id']])->find();
            $face = $limit_change_age ? 1 : $face;
        }

        $is_face = 0;
        if ($face){
            $FaceAuth = new FaceAuth;
            $authpic = Db::name('auth')->where(['user_id'=>$this->member['id']])->value('image1');
            if(!$authpic){
                return $this->miss('未真人认证，无法上传人脸匹配图片到头像');
            }
            $path = getenv('APP_OSS') ? 'http://' . getenv('OSS_BUCKET') . '.' . getenv('OSS_ENDPOINT') : SITE_PATH;
            $authpic = (strstr(PHP_OS, 'WIN') ? 'http://' . getenv('APP_URL') : $path) . $authpic;

            try {
                $FAres = $FaceAuth->comparebaidu($path . $head, $authpic);
            } catch (\Exception $e) {
                $FAres = true;
            }
            if($FAres){
                return $this->miss('本次更换头像与真人认证上传图人脸不符，请重新上传');
            } else {
                $is_face = 1;
            }
        }

        if ($config['check_avatar']) {
            return $this->return['common.success.check'];
        } else {
            UserMaterialVerify::index(6, $this->member['id'], $this->params['head'], $is_face, 1);

            $data = [
                'head_portrait' => $head,
                'real_avatar' => $is_face,
            ];

            UserProfile::change($this->member['id'], $data);

            return $this->return['common.success.update'];
        }
    }

    public function editInfo()
    {
        $this->params['user_id'] = $this->member['id'];
        $success_text = '修改成功';
        $config = Config::index('check');
        $contain = ['id', 'head_portrait', 'user_id', 'sex', 'age', 'app_id', 'phone', 'real_avatar', 'create_time', 'update_time', 'proxy', 'pull_time', 'pull_line_count', 'partner', 'fill_rate', 'matchmaker_id', 'is_chosen'];
        // 基本资料
        $info = Db::getTableInfo('user_profile', 'fields');
        $info = array_diff($info, $contain);

        $minganci = Db::name('filter_word')->where(['state' => 1])->column('name');

        $fill_one_rate = 1 / sizeof($info) * 100;
        $fill_rate = 0;

        foreach ($this->params as $key => $value) {
            if (in_array($key, $contain)) {
                unset($this->params[$key]);
            }

            if(in_array($key, ['name', 'introduction'])){
                $keynames = ['name'=>'昵称','introduction'=>'自我介绍'];


                if(ishasPhoneNumber($value)){
                    return $this->miss($keynames[$key] . '禁止输入手机号');
                }


                foreach ($minganci as $v) {
                    if (stripos($value, $v) !== false) {
                        return $this->miss($keynames[$key] . '含有敏感词"' . $v . '",请重新编辑');
                    }
                }
                // if ($key == 'name' && empty($value)) {
                //     unset($this->params['name']);
                // }
            }

            if ($key == 'occupation') {
                if ($value) {
                    $hasoccupation = Db::name('user_occupation')->where(['name'=>$value])->find();
                    if (!$hasoccupation) unset($this->params['occupation']);
                }
            }
        }

        if (isset($this->params['name']) && $this->params['name'] != $this->member['name'] && isset($config['check_edit_nickname']) && $config['check_edit_nickname'] == 1) {

            $limit = UserDeedLimit::check($this->member['id'], 3);
            if ($limit) {
                return $this->miss('当前已被限制修改昵称');
            }
            UserMaterialVerify::index(7, $this->member['id'], $this->params['name']);
        }
        if (isset($this->params['slogan']) && $this->params['slogan'] && isset($config['check_edit_slogan']) && $config['check_edit_slogan'] == 1) {
            UserMaterialVerify::index(8, $this->member['id'], $this->params['slogan']);
        }

        $userProfile = Db::name('user_profile')->where(['user_id'=>$this->member['id']])->field(implode(',',$contain), 'filter')->find();
        $userProfile = array_merge($userProfile, $this->params);
        foreach ($userProfile as $key => $value) {
            if ($value) {
                $fill_rate += $fill_one_rate;
            }
        }
        $this->params['fill_rate'] = $fill_rate;

        if (isset($this->params['name']) && !$this->params['name'] && !$userProfile['name']) {
            $this->params['name'] = suiji2();
        }

        UserProfile::change($this->member['id'], $this->params);

        return $this->pass($success_text, 'msg', $this->params);
    }

    public function vip_style()
    {
        $data = Db::name('hunjie_vip_style')
            ->where(['state'=>1])
            ->order('sort asc,id asc')
            ->field('app_id, assists, state, sort', 'filter')
            ->select();

        $durations = ['180'=>'6个月', '365'=>'12个月'];
        foreach ($data as &$value) {
            $value['duration'] = $durations[$value['day']];
        }
        return $this->pass($data);
    }


    public function vip($w2 = 0)
    {
        $config = Config::index('', 'vip_hunjie');
        $vip_hunjie = $config['vip_hunjie'];
        if (!$vip_hunjie) {
            return $this->miss('暂未开放购买');
        }
        $id = input('id/d', 0);
        $vip_style = Db::name('hunjie_vip_style')->where(['id'=>$id, 'state'=>1])->find();
        if (!$vip_hunjie) return $this->miss('套餐不存在');

        if ($this->params['method'] == 1) {
        } else {
            $order = OrderCommon::index(8, $this->member['id'], $vip_style['price'], $this->params['method'], json_encode(['id' => $id]));

            if (isset($this->params['h5']) && $this->params['h5']) {
                return $this->pass(['order_sn' => $order['order_sn'], 'url' => 'api/wap/pay']);
            } else {
                $action = $this->params['method'] == 3 ? 'api/Wechat/app' : 'api/Alipay/tradeAppPay';
                if ($w2 && $action == 'api/Wechat/app') {
                    $action = 'api/Wechat2/app';
                }
            }

            return action($action, ['order_sn' => $order['order_sn']]);
        }
    }

    public function store()
    {
        $page = input('page/d', 1);
        $pagesize = input('pagesize/d', 1);
        $data = Db::name('hunjie_store')
            ->where(['state'=>1])
            ->field('id,app_id,state', 'filter')
            ->page($page)
            ->limit($pagesize)
            ->select();
        return $this->pass($data);
    }



    public function partner_lists()
    {
        $data = Db::name('hunjie_partner')
            ->field('id, type, name, price')
            ->order('sort desc')
            ->select();

        return $this->pass($data);
    }

    public function partner_join()
    {
        $id = input('id/d', 0);
        $w2 = input('w2/d', 0);
        $area = input('area/s', '');

        $userProfile = Db::name('user_profile')->where(['user_id'=>$this->member['id']])->find();
        if (!$userProfile['proxy']) {
            return $this->miss('请先开通婚介');
        }
        if ($userProfile['partner']) {
            return $this->miss('你已是合伙人');
        }

        $hasApply = Db::name('hunjie_partner_apply')->where(['user_id'=>$this->member['id'], 'status'=>0])->find();
        if ($hasApply) return $this->miss('已申请过,等待审核');

        $style = Db::name('hunjie_partner')->find($id);
        if (!$style) return $this->miss('操作异常');

        if ($style['min'] > $this->member['shouyi']) {
            return $this->miss('收益需达到 ' . $style['min'] . ' 才能申请');
        }

        switch ($this->params['method']) {
            default:
                $order = OrderCommon::index(10, $this->member['id'], $style['price'], $this->params['method'], json_encode(['type' => $style['type'], 'area'=>$area], JSON_UNESCAPED_UNICODE));

                if (isset($this->params['h5']) && $this->params['h5']) {
                    return $this->pass(['order_sn' => $order['order_sn'], 'url' => 'api/wap/pay']);
                } else {
                    $action = $this->params['method'] == 3 ? 'api/Wechat/app' : 'api/Alipay/tradeAppPay';
                    if ($w2 && $action == 'api/Wechat/app') {
                        $action = 'api/Wechat2/app';
                    }
                }

                return action($action, ['order_sn' => $order['order_sn']]);
        }
    }


    public function matchmaking()
    {
        $w2 = input('w2/d', 0);
        $userProfile = Db::name('user_profile')->where(['user_id'=>$this->member['id']])->find();
        if ($userProfile['proxy']) {
            return $this->miss('您已开通婚介');
        }

        $price = Config::where(['title' => 'hunjie_join'])->value('content');

        $data = [
            'name' => $this->params['name'],
            'city' => $this->params['city'],
            'phone' => $this->params['phone'],
            'idcard' => $this->params['idcard'],
        ];

        $data = json_encode($data,JSON_UNESCAPED_UNICODE);

        if ($price + 0) {
            switch ($this->params['method']) {
                default:
                    $order = OrderCommon::index(9, $this->member['id'], $price, $this->params['method'], $data);
                    if (isset($this->params['h5']) && $this->params['h5']) {
                        return $this->pass(['order_sn' => $order['order_sn'], 'url' => 'api/wap/pay']);
                    } else {
                        $action = $this->params['method'] == 3 ? 'api/Wechat/app' : 'api/Alipay/tradeAppPay';
                        if ($w2 && $action == 'api/Wechat/app') {
                            $action = 'api/Wechat2/app';
                        }
                    }

                    return action($action, ['order_sn' => $order['order_sn']]);
            }
        }
    }

    public function income_static()
    {
        $data = Db::name('user_data')->where(['user_id'=>$this->member['id']])->field('income_today,income_month,income')->find();
        if (!$data) {
            $data = [
                'income_today'=>0,
                'income_month'=>0,
                'income'=>0,
            ];
        }
        return $this->pass($data);
    }


    public function income()
    {
        $page = input('page/d', 1);
        $pagesize = input('pagesize/d', 20);
        $time = input('time/s', 'd');
        $pagesize = $pagesize > 20 ? 20 : $pagesize;
        $times = ['d'=>'today', 'm'=>'month'];

        $data = Db::name('yaoqing_records')
            ->field('show_money money,content,FROM_UNIXTIME(time,"%Y-%m-%d %H:%i") time')
            ->where(['uid' => $this->member['id']])
            ->whereTime('time', $times[$time] ?? $times['d'])
            ->order('id desc')
            ->page($page)
            ->limit($pagesize)
            ->select();

        return $this->pass($data);
    }
}